<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: ProductController.php 4929 2010-08-25 03:36:50Z jam-gate.vn $
 * @since		2.0.0
 */

class Product_ProductController extends Zend_Controller_Action 
{
	/* ========== Backend actions =========================================== */

	/**
	 * Add new product
	 * 
	 * @return void
	 */
	public function addAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('product')->getProductDao();
		$productDao->setDbConnection($conn);
		
		$nextId 	= 	$productDao->getNextId();
		$this->view->assign("nextId",$nextId);
		
		$product = new stdClass();
		$product->id		 		 = $nextId;
		$product->shop_id 			 = Zend_Auth::getInstance()->getIdentity()->shop->id;
		$product->product_name_vi_VN = Tomato_Config::getConfig()->get("jam")->product_name_vi_VN;
		$product->product_name_en_US = Tomato_Config::getConfig()->get("jam")->product_name_en_US;
		$product->amount			 = 0;
		$product->original_price	 = 0;
		$product->sale_price		 = 0;
		$product->product_unit		 = "";
		$product->product_unit_use   = "";
		$product->volume_one 		 = 1;
		$product->images 		 	 = "";
		$product->note_used			 = "";
		$product->block			 	 = 0;
		$request  = $this->getRequest();
		
		$this->view->assign('lang', $request->getParam('lang'));
		
		if ($request->isPost()) {
			$user 	  = Zend_Auth::getInstance()->getIdentity();
			$product->product_name_vi_VN = $request->getParam("product_name_vi_VN",$product->product_name_vi_VN);
			$product->product_name_en_US = $request->getParam("product_name_en_US",$product->product_name_en_US);
			// đăng kí sản phẩm mới số lượng là 0
			$product->amount			 = 0;
			$product->original_price	 = $request->getParam("original_price",$product->original_price);
			$product->sale_price		 = $request->getParam("sale_price",$product->sale_price);
			$product->product_unit		 = $request->getParam("product_unit",$product->product_unit);
			$product->product_unit_use   = $request->getParam("product_unit_use",$product->product_unit_use);
			$product->volume_one 		 = $request->getParam("volume_one",$product->volume_one);
			$product->volume_total 		 = $request->getParam("volume_total",$product->volume_one * $product->amount);
			$product->images 		 	 = $request->getParam("images",$product->images);
			$product->note_used 		 = $request->getParam("note_used",$product->note_used);
			$id = $productDao->add($product);			
			$this->_helper->getHelper('FlashMessenger')
				->addMessage($this->view->translator('product_add_success'));
			$this->_redirect($this->view->serverUrl() . $this->view->url(array("product_id"=>$product->id), 'product_product_edit'));
		}
		$this->view->assign("product",$product);
	}
	
	/**
	 * Delete product
	 * 
	 * @return void
	 */
	public function deleteAction() 
	{
		$this->_helper->getHelper('layout')->disableLayout();
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		
		$response = 'RESULT_ERROR';
		$request  = $this->getRequest();
		if ($request->isPost()) {
			$id = $request->getPost('id');
			$conn = Tomato_Db_Connection::factory()->getMasterConnection();
			$productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('product')->getProductDao();
			$productDao->setDbConnection($conn);
			$product = $productDao->getById($id);
			
			if ($product != null) {
				// delete resource file				
				$dir  = 'resource'.DS.substr(Tomato_Config::getConfig()->get("db")->prefix,0,-1).DS.$product->shop_id.DS.'images'.DS.'products'.DS.$product->id; 
				if(file_exists($dir)){
					Tomato_Utility_File::deleteRescursiveDir(TOMATO_ROOT_DIR.DS.$dir);
				}
				$productDao->delete($product);
				$response = 'RESULT_OK';
			}
		}
		$this->getResponse()->setBody($response);
	}
	
	/**
	 * Edit product
	 * 
	 * @return void
	 */
	public function editAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('product')->getProductDao();
		$productDao->setDbConnection($conn);
		$request  = $this->getRequest();		
		$product_id = $request->getParam("product_id",null);
		if($product_id){
			$product 	= 	$productDao->getById($product_id);
			if($product){				
				$this->view->assign('lang', $request->getParam('lang'));
				
				if ($request->isPost()) {
					$user 	  = Zend_Auth::getInstance()->getIdentity();
					$product->product_name_vi_VN = $request->getParam("product_name_vi_VN",$product->product_name_vi_VN);
					$product->product_name_en_US = $request->getParam("product_name_en_US",$product->product_name_en_US);
					if($product->block<1){
						$product->amount			 = $request->getParam("amount",$product->amount);
						$product->original_price	 = $request->getParam("original_price",$product->original_price);
						$product->sale_price		 = $request->getParam("sale_price",$product->sale_price);
						$product->volume_one 		 = $request->getParam("volume_one",$product->volume_one);
						$product->volume_total 	 = $request->getParam("volume_total",$product->volume_one * $product->amount);
					}					
					$product->product_unit		 = $request->getParam("product_unit",$product->product_unit);
					$product->product_unit_use   = $request->getParam("product_unit_use",$product->product_unit_use);
					$product->images 		 	 = $request->getParam("images",$product->images);
					$product->note_used			 = $request->getParam("note_used",$product->note_used);
						
					$id = $productDao->update($product);			
					$this->_helper->getHelper('FlashMessenger')
						->addMessage($this->view->translator('product_edit_success'));
					$this->_redirect($this->view->serverUrl() . $this->view->url(array("product_id"=>$product->id), 'product_product_edit'));
				}
				$this->view->assign("product",$product);
			}else{
				Core_Services_Deny::deny($this->view->translator("product_deny_id_non_exists"));
			}
		}
	}
	
	/**
	 * List categories
	 * 
	 * @return void
	 */
	public function listAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('product')->getProductDao();
		$productDao->setDbConnection($conn);
		
		$lang = $this->getRequest()->getParam('lang', Tomato_Config::getConfig()->web->lang);
		
		$productDao->setLang($lang);
        $request   = $this->getRequest();
		$pageIndex = $request->getParam('pageIndex', 1);
        $perPage   = Tomato_Config::getConfig()->manager->itemPerPage;;
        $offset    = ($pageIndex - 1) * $perPage;
        $exp = array(
        	   );
		$products 	 = $productDao->find($offset, $perPage, $exp);
		$this->view->assign('products', $products);
		
		$total       = $productDao->count($exp);        
		$paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($products, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);
        
		$this->view->assign('paginator', $paginator);
        $this->view->assign('paginatorOptions', array(
            'path'        => $this->view->url(array(), 'product_product_list'),
            'itemLink' 	  => 'page-%d'                          
        ));
        $this->view->assign('lang',$lang);				
	} 		
}	